<?php
Prado::using('Application.common.common');
class CartItemTemplate extends TRepeaterItemRenderer
{
	public function onDataBinding($param)
	{
		parent::onDataBinding($param);
		$productRecord = ProductRecord::finder()->findByPk($this->Data->ProductID);
		if ($productRecord instanceof ProductRecord)
		{
			$this->lnkProductName->Text = $productRecord->Name;
			$this->lnkProductName->NavigateUrl = $this->Service->ConstructUrl('shop.ProductDetails',array('id'=>$productRecord->ID,'alias'=>$productRecord->Alias));
			$this->lblProductPrice->Text = Common::getFormattedValue($productRecord->Price);
			$this->lblProductBrief->Text = $productRecord->Brief."...";
			$this->lnkProductImage->NavigateUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/product/".$productRecord->ImagePath;
			$this->lnkProductImage->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/product/thumbs/".$productRecord->ThumbnailPath;
		}
		$this->lblSubTotal->Text = Common::getFormattedValue($this->Data->Subtotal);
	}
	
	protected function btnUpdate_Clicked($sender, $param)
	{
		$cartDetail = CartTempDetailRecord::finder()->findByPk($this->txtID->Value);
		if ($cartDetail instanceof CartTempDetailRecord)
		{
			$cartDetail->Quantity = TPropertyValue::ensureInteger($this->txtQuantity->SafeText);
			$cartDetail->Subtotal = $cartDetail->Quantity*Common::roundTo($cartDetail->ProductPrice);
			$cartDetail->save();
		}
		try {
			$this->Page->renderBoxShippingMethod();	
			$this->Page->populateCartDetails();
		}
		catch(TException $e) { echo $e;}
		
		
	}
	
	protected function btnDelete_Clicked($sender, $param)
	{
		$cartDetail = CartTempDetailRecord::finder()->findByPk($this->txtID->Value);
		$cartDetail->delete();
		try {
			$this->Page->renderBoxShippingMethod();	
			$this->Page->populateCartDetails();
		}
		catch(TException $e) {echo $e;}
	}
}
?>